package Connection;

import BasicClass.Record;
import BasicClass.User;
import DAO.DaoException;
import DAO.RecordDao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class RecordDaoImpl implements RecordDao {
    static MyConnection Connection;
    public RecordDaoImpl(MyConnection myConnection){
        Connection=myConnection;
    }

    @Override
    public void addRecord(Record record) throws DaoException {
        String getCount = "SELECT COUNT(*) FROM record";
        String sql = "INSERT INTO record VALUE(?, ?, ?, ?, ?, ?, ?, ?)";
        int count = 0;
        ResultSet rst;
        try{
            PreparedStatement pstmt = Connection.getConnection().prepareStatement(getCount);
            rst = pstmt.executeQuery();
            if(rst.next()){
                count = rst.getInt(1);
                pstmt = Connection.getConnection().prepareStatement(sql);
                pstmt.setInt(1, count+1);
                pstmt.setString(2, record.getAccountNumber());
                pstmt.setString(3, record.getISBN());
                pstmt.setInt(4, record.getNumber());
                pstmt.setString(5, record.getLoanTime());
                pstmt.setString(6, record.getReturnTime());
                pstmt.setString(7, null);
                pstmt.setString(8, record.getState());
                pstmt.executeUpdate();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateRecord(Record record, int[] ChangeList) throws DaoException {
        String sql  = "UPDATE V_record SET R_Id = ?";
        int rst;
        String[] property = new String[10];
        property[0]="R_Id";property[1]="C_Number";property[2]="C_Name";
        property[3]="B_ISBN";property[4]="B_Name";property[5]="R_Number";
        property[6]="R_LoanTime";property[7]="R_ReturnTime";property[8]="R_FinishTime";
        property[9]="R_State";
        String[] recordProperty = record.toString().split(",", -1);
        for (int i = 0; i < ChangeList.length; i++) {
            if(ChangeList[i]==1){
                sql+=","+property[i]+"=";
                if(i!=0&i!=5)sql+="'"+recordProperty[i]+"'";
                else sql+=recordProperty;
            }
        }
        sql += " WHERE R_Id = ?";
        try{
            PreparedStatement pstmt = Connection.getConnection().prepareStatement(sql);
            pstmt.setInt(1, record.getId());
            pstmt.setInt(2, record.getId());
            rst = pstmt.executeUpdate();
            if(rst==1){}
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteRecord(Record record) throws DaoException {

    }

    @Override
    public ArrayList<Record> getRecord(User Account, Record record, int choose) throws DaoException {
        ArrayList<Record> records = new ArrayList<>();
        String sql = "SELECT * FROM v_record WHERE C_Number = ? AND R_State = '借阅中'";
        if(choose==1) sql += " AND B_Name = ?";
        else if(choose==2) sql += " AND R_Id = ?";
        ResultSet rst;
        try{
            PreparedStatement pstmt = Connection.getConnection().prepareStatement(sql);
            pstmt.setString(1, Account.getNumber());
            if(choose==1) pstmt.setString(2, record.getBookName());
            else if(choose==2) pstmt.setInt(2, record.getId());
            rst = pstmt.executeQuery();
            while(rst.next()){
                record = new Record(
                        rst.getInt(1),rst.getString(2),rst.getString(3),
                        rst.getString(4),rst.getString(5),rst.getInt(6),
                        rst.getString(7),rst.getString(8),rst.getString(9),
                        rst.getString(10));
                records.add(record);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return records;
    }

    @Override
    public ArrayList<Record> getAllRecord() throws DaoException {
        return null;
    }
}
